#include "netprotocol.h"
#include "datatemp.h"
#include "netdriver.h"
#include <QJsonObject>
#include <QJsonDocument>
#include "netdriver.h"
#include "serialportprotocol.h"
#include "datatemp.h"
#include <QDebug>
NetProtocol *NetProtocol::obj= nullptr;

NetProtocol::NetProtocol(QObject *parent) : QObject(parent)
{
connect(NetDriver::getObject(),&NetDriver::netRead,this,&NetProtocol::NetReadSlot);
}

NetProtocol *NetProtocol::getObject()
{
    if(obj == nullptr)
            obj = new NetProtocol;
    return obj;
}

bool NetProtocol::deviceLogin(QString id, QString key)
{
    QJsonObject obj;
    obj.insert("type",8000);
    obj.insert("index",id);
    obj.insert("password",key);
    bool ok=NetDriver::getObject()->netWrite(QJsonDocument(obj).toJson());
    return ok;
}


bool NetProtocol::uploadSensorData(int boardId, double tem, double hum1, double hum2,
                                   int light, double press, bool water1, bool water2)
{
    QJsonObject obj;
    obj.insert("type",1001);
    obj.insert("index",DataTemp::getObject()->deviceId);
    obj.insert("identification",DataTemp::getObject()->identification);
    obj.insert("boardid",boardId);
    obj.insert("tem",tem);
    obj.insert("hum1",hum1);
    obj.insert("hum2",hum2);
    obj.insert("light",light);
    obj.insert("press",press);
    obj.insert("water1",water1);
    obj.insert("water2",water2);
    bool ok = NetDriver::getObject()->netWrite(QJsonDocument(obj).toJson());
    return ok;
}



void NetProtocol::NetReadSlot(QByteArray data)
{
    qDebug() << "yijingshoudaole";

    int count=0;
    //qDebug() << QString::fromLocal8Bit(data);
    for(int i = 0;i < data.length();i++)
    {
        if(data.at(i) == '{')
            count++;
        else if(data.at(i) == '}'){
            count--;
            if(count == 0){
                QByteArray temp = data.mid(0,i+1);
                //qDebug() << temp;
                handleFrame(temp);
                data = data.mid(i+1);
                i= -1;
                      }
        }
    }
}
void NetProtocol::handleFrame(const QByteArray &temp)
{
    QJsonObject obj =
        QJsonDocument::fromJson(temp).object();
   int type = obj.value("type").toInt();
   qDebug () << "re==" << type;
   if(type == 8001)
   {
     LoginFeedBack(obj);
   }
   else if(type == 2001)
   {

   }
   else if(type == 2002)
   {

   }
   else if(type == 2003)
   {
    controlNormalDevice(obj);
   }
   else if(type == 1002)
   {

   }
   else if(type == 10000)
   {
      deviceLogin(DataTemp::getObject()->deviceId,DataTemp::getObject()->deviceKey);
   }

}

void NetProtocol::LoginFeedBack(const QJsonObject &obj)
{
bool re = obj.value("re").toBool();
if(!re)
{
    return;
}
QString identification = obj.value("identification").toString();
qDebug() << "identification==" << identification;
DataTemp::getObject()->identification=identification;//将身份识别码保存
QString time = obj.value("time").toString();
QString message=QString("loginFeedBack:%1  time:%2").arg(re).arg(time);
emit  showMessage(message);
}



void NetProtocol::controlNormalDevice(const QJsonObject &obj)
{
 int id= obj.value("id").toInt();
 bool re=obj.value("sw").toBool();
 switch (id) {
 case 1:
     if(re)
     SerialPortProtocol::getObject()->openWater1(1);
     else
     SerialPortProtocol::getObject()->closeWater1(1);

     break;
 case2:
     if(re)
     SerialPortProtocol::getObject()->openWater2(1);
     else
     SerialPortProtocol::getObject()->closeWater2(1);

     break;
 case 3:
     if(re)
     SerialPortProtocol::getObject()->openWater1(2);
     else
     SerialPortProtocol::getObject()->closeWater1(2);

     break;
 case 4:
     if(re)
     SerialPortProtocol::getObject()->openWater2(2);
     else
     SerialPortProtocol::getObject()->closeWater2(2);

     break;

 default:
     break;
 }

}
